package com.macro.mall.portal.service.impl;

import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.OmsDistributionStoreOrderService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class OmsDistributionStoreOrderServiceImpl implements OmsDistributionStoreOrderService {
    @Autowired
    private OmsDistributionStoreOrderMapper omsDistributionStoreOrderMapper;
    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private OmsOrderSettingMapper omsOrderSettingMapper;
    @Override
    public int insertByOrderId(Long orderId) {
        OmsOrderSettingExample settingExample = new OmsOrderSettingExample();
        OmsOrderSetting omsOrderSetting =  omsOrderSettingMapper.selectByExample(settingExample).get(0);
        if (orderId != null) {
            OmsOrderItemExample example = new OmsOrderItemExample();
            example.createCriteria().andOrderIdEqualTo(orderId).andStatusEqualTo(3);
            OmsOrder omsOrder = this.orderMapper.selectByPrimaryKey(orderId);
            OmsDistributionStoreOrder order = new OmsDistributionStoreOrder();
            order.setCreateTime(new Date());
            order.setMemberId(omsOrder.getMemberId());
            order.setOrderId(omsOrder.getId());
            order.setOrderSn(omsOrder.getOrderSn());
            order.setStatus(OmsDistributionOrder.STATUS.FINISH.value());
            order.setStoreId(omsOrder.getUserStoreId());
            order.setStoreUserId(omsOrder.getUserId());
            BigDecimal amount = new BigDecimal(100 - omsOrderSetting.getPlatformCommission()).multiply(omsOrder.getPayAmount()).divide((BigDecimal.TEN.multiply(BigDecimal.TEN)), 2, BigDecimal.ROUND_HALF_UP);
            if (amount.compareTo(BigDecimal.ZERO) == 0) {
                return 0;
            }
            order.setAmount(amount);
            this.omsDistributionStoreOrderMapper.insert(order);
        }
        return 0;
    }

}
